package cn.zktr.Personnel.controllers.tantiantian;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.zktr.Personnel.model.pojos.PerPmsEmp;
import cn.zktr.Personnel.model.pojos.PerSlyScsy;
import cn.zktr.Personnel.model.pojos.PerSlySls;
import cn.zktr.Personnel.model.pojos.PerSlySz;
import cn.zktr.Personnel.model.pojos.PerSlyTsx;
import cn.zktr.Personnel.model.service.tantiantian.SlySetService;
import cn.zktr.Personnel.model.service.tantiantian.SlySlService;
import cn.zktr.Personnel.model.uitls.Homapape;

@Controller
public class SalarypaymentController {
	Map<String,Object> map;
	/**
	 * 薪资套帐
	 */
	@Autowired
	private SlySetService slyser;
	
	/**
	 * 薪资发放
	 */
	@Autowired
	private SlySlService slser;
	
	//薪资发放
	@RequestMapping("ttt/salary-payment")
	public void Salarypayment(Model model,HttpSession session){
		System.err.println("进来了第一次");
		//查询所有薪资套账
		JSONArray a=slyser.selectSlyAll();
		model.addAttribute("payment_", a);
		//查询所有社保套账
		JSONArray sz = slser.seleSz();
		model.addAttribute("sz_",sz);
		//查询所有财务上级（有权限审批）
		JSONArray seleStmpotid=slyser.seleStmpotid(session);
		model.addAttribute("payment_shenmpi", seleStmpotid);
		
	} 
	
	/**
	 * @param model薪资套账下拉
	 * @return
	 */
	@RequestMapping("pament_shenpiren")
	@ResponseBody
	public JSONArray pament_shenpiren(Model model){
		return  slyser.selectSlyAll();
		
	} 
	/**
	 * @param model审批人下拉payment_shenmpi
	 * @return
	 */
	@RequestMapping("payment_shenmpi")
	@ResponseBody
	public JSONArray payment_shenmpi(Model model,HttpSession session){
		return  slyser.seleStmpotid(session);
		
	}
	//
	//模糊查询薪资发放Paymenttable_ById
	@RequestMapping("Paymenttable_ById")
	@ResponseBody
	public Map<String,Object> Paymenttable_ById(int page,int rows,String text){
		System.err.println(text);
		Map<String,Object> map = new HashMap<>();
		map.put("total",slser.seleSlBy_(text).size());
		Page<Object> p=PageHelper.startPage(page,rows);
		JSONArray slby_=slser.seleSlBy_(text);
		//页内容
		map.put("rows",slby_);

		return map;
	}
	/**
	 * @param page
	 * @param rows
	 * @return查询所有薪资套帐
	 */
	@RequestMapping("Paymenttable")
	@ResponseBody
	public Map<String,Object> SalaryAccounttable(int page,int rows,Model session,HttpSession sessions){
		//查询所有薪资套账
		JSONArray a=slyser.selectSlyAll();
		session.addAttribute("payment_", a);
		//查询所有社保套账
		JSONArray sz = slser.seleSz();
		session.addAttribute("sz_",sz);
		//查询所有财务上级（有权限审批）
		JSONArray seleStmpotid=slyser.seleStmpotid(sessions);
		session.addAttribute("payment_shenmpi", seleStmpotid);
		
		
		Map<String,Object> map = new HashMap<>();
		map.put("total",slser.seleSlAll().size());
		Page<Object> p=PageHelper.startPage(page,rows);
		JSONArray findAll=slser.seleSlAll();
		//页内容
		map.put("rows",findAll);

		return map;
	}
	/**
	 * @param model社保下拉SocialSecurity_s
	 * @return
	 */
	@RequestMapping("SocialSecurity_s")
	@ResponseBody
	public JSONArray SocialSecurity_s(Model model){
		return slser.seleSz();
		
	}
	/**
	 * @param setid
	 * @return根据套帐id查询套帐-员工表（中间表）下的所有员工
	 */
	@RequestMapping("Payment_setid")
	@ResponseBody
	public void Payment_setid(String setId){
		map = new HashMap<>();
		if(setId!="") {
			JSONArray Ce=slser.seleceAll(Integer.parseInt(setId));
				map.put("rows",Ce);	
		}else {
			System.err.println("setid不能为空");
		}
		
		
		//页内容
		
	}
	@RequestMapping("payment_yuangong")
	@ResponseBody
	public Map<String,Object> payment_yuangong(){
		return map;	
	}
	//Payment_sl
	@RequestMapping("Payment_sl")
	@ResponseBody
	public void Payment_sl(
			PerSlySls sls,
			Integer perSlySets,
			Integer perSlySzs,
			String[] empid,
			Integer perPmsEmp2,
			HttpSession session
			
	){
		slser.addSls( sls, perSlySets,perPmsEmp2, session);
		slser.addWagEmp(empid,perSlySzs,perSlySets,session);
	}
	//薪资明细
	@RequestMapping("pay_yuagongmingxi")
	@ResponseBody
	public List<JSONObject> pay_yuagongmingxi(Integer id,HttpSession session){ 
			return slser.seleByslsId(id);
	}
	//查看薪资单pay_chakan
	@RequestMapping("pay_chakan")
	@ResponseBody
	public String pay_chakan(Integer id){ 
		JSONArray s=slser.pay_chakan(id);
		if(s.size()>0) {
			String name="";
			 for (int i = 0; i < s.size(); i++) {
				 JSONObject ob=(JSONObject) s.get(i);
				 name+=ob.get("EMPNAME")+",";
			 }
				return name.substring(0,name.length()-1);
		}else {
			System.err.println("薪资单无数据");
			return null;
		}
	}
	//薪资发放送审Account_fafang_songshen
	@RequestMapping("Account_fafang_songshen")
	@ResponseBody
	public void Account_fafang_songshen(Integer slsid){ 
		slser.Account_fafang_songshen(slsid);
	}
	//薪资发放
	@RequestMapping("Account_fafang")
	@ResponseBody
	public void Account_fafang (Integer slsid)throws HttpException, IOException{
		slser.fafang(slsid);
	}
	//查看个人薪资sele_mysal
	@RequestMapping("sele_mysal")
	@ResponseBody
	public JSONArray sele_mysal (HttpSession session){
		return slser.sele_mysal(session);
	}
	@RequestMapping("sele_mysals")
	@ResponseBody
	public  Map<String,Object>  sele_mysals(Integer empid,int page,int rows){
		Map<String,Object> map = new HashMap<>();
		slser.sele_sele_mysals(empid);
		if(slser.sele_sele_mysals(empid).size()>0) {
			map.put("total",slser.sele_sele_mysals(empid).size());
			Page<Object> p=PageHelper.startPage(page,rows);
			JSONArray findAll=slser.sele_sele_mysals(empid);
			//页内容
			map.put("rows",findAll);
			return map;
		}else if(slser.sele_sele_mysals(empid).size()<=0) {
				map.put("total",slser.sele_xinzichaxun().size());
				Page<Object> p=PageHelper.startPage(page,rows);
				JSONArray findAll=slser.sele_xinzichaxun();
				//页内容
				map.put("rows",findAll);
				return map;
			}
		
		return null;
		
		
	}
	
	//查看所有薪资
	@RequestMapping("xinzichaxun_")
	@ResponseBody
	public Map<String,Object> xinzichaxun_(int page,int rows){
		Map<String,Object> map = new HashMap<>();
		map.put("total",slser.sele_xinzichaxun().size());
		Page<Object> p=PageHelper.startPage(page,rows);
		JSONArray findAll=slser.sele_xinzichaxun();
		//页内容
		map.put("rows",findAll);
		return map;
	}
	//根据slsid查看社保
	@RequestMapping("pay_chakanshebao")
	@ResponseBody
	public String pay_chakanshebao (Integer slsid){
		System.err.println(slsid);
		JSONObject ob=slser.pay_chakanshebao(slsid).getJSONObject(0);
		if(ob.size()<=0) {
			return "无";
		}
		return ob.get("SZNAME").toString();
	}
	//禁用Account_jinyong
	@RequestMapping("pay_jinyong")
	@ResponseBody
	public void pay_jinyong(Integer slsid){ 
		slser.jinyong(slsid);
	}
}
